package leetcode;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2021-11-17 14:46
 **/
public class LeetCode264 {

    public int nthUglyNumber(int n) {
        //dp[i]:第i个丑数
        int[] dp = new int[n + 1];
        int a = 1;
        int b = 1;
        int c = 1;
        dp[1] = 1;
        for (int i = 2; i <= n; i++) {
            int n1 = dp[a] * 2;
            int n2 = dp[b] * 3;
            int n3 = dp[c] * 5;
            dp[i] = Math.min(Math.min(n1, n2), n3);
            if (dp[i] == n1) {
                a++;
            }
            if (dp[i] == n2) {
                b++;
            }
            if (dp[i] == n3) {
                c++;
            }
        }
        return dp[n];
    }

}
